// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { error } from "console";
import type { NextApiRequest, NextApiResponse } from "next";
import {postAnswer} from "@/services/answer"

function getAnswerInfo(reqBody:any){
  const answerList:any[] =[]
  Object.keys(reqBody).forEach(key=>{
    if(key === 'questionId') return
    answerList.push({
      componentId:key,
      value:reqBody[key]
    })
  })
  return{
    questionId:reqBody.questionId||'',
    answerList
  }
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
 if(req.method !== 'POST'){
  res.status(200).json({error:-1,msg:'Method 错误'})
 }
 const answerInfo = getAnswerInfo(req.body)
 try {
  const resData = await postAnswer(answerInfo)
  if(resData.errno ===0){
    res.redirect('/success')
  }
  else{
    res.redirect('/fail')
  }
 
 } catch (err){
  res.redirect('/fail')
 }
 res.status(200).json({error:0})
}
